package com.xwj.poi;

import com.xwj.entity.UserInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.StringUtils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PoIExcelOutPut {
    private Workbook wb;
    private FileInputStream inputStream;
    private String[] title = {"账号", "用户名", "密码", "性别", "年龄", "电话", "邮件", "地址", "头像"};

    public PoIExcelOutPut(String xlsPath) throws IOException {
        //根据指定的文件输入流导入Excel从而产生Workbook对象
        try {
            inputStream = new FileInputStream(xlsPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //兼容高低版本
        if (xlsPath.endsWith("xls")) {
            wb = new HSSFWorkbook(inputStream);

        } else if (xlsPath.endsWith("xlsx")) {
            wb = new XSSFWorkbook(inputStream);
        }
    }

    public List<UserInfo> loadNoticeInfo() throws IOException {
        List temp = new ArrayList();
        boolean flag = false;
        Sheet sheet = wb.getSheetAt(0);
        int cellnum = sheet.getRow(0).getPhysicalNumberOfCells();
        if (cellnum == title.length) {
            for (int i = 0; i < title.length; i++) {
                if (!sheet.getRow(0).getCell(i).getStringCellValue().equals(title[i])) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                for (Row r : sheet) {//从第二行开始读
                    if (r.getRowNum() < 1) {
                        continue;
                    }
                    for (int i = 0; i < cellnum; i++) {
                        r.getCell(i).setCellType(Cell.CELL_TYPE_STRING);
                    }
                    String accountid = r.getCell(0).getStringCellValue();
                    String username = r.getCell(1).getStringCellValue();
                    String password = r.getCell(2).getStringCellValue();
                    String sex = r.getCell(3).getStringCellValue();
                    String age = r.getCell(4).getStringCellValue();
                    String phone = r.getCell(5).getStringCellValue();
                    String mail = r.getCell(6).getStringCellValue();
                    String address = r.getCell(7).getStringCellValue();
                    String photo = r.getCell(8).getStringCellValue();
                    UserInfo userInfo = new UserInfo();
                    if (!StringUtils.isEmpty(accountid) && !StringUtils.isEmpty(username) && !StringUtils.isEmpty(sex)
                            && !StringUtils.isEmpty(age) && !StringUtils.isEmpty(phone) && !StringUtils.isEmpty(mail)
                            && !StringUtils.isEmpty(address)) {
                        userInfo.setAccountid(accountid);
                        userInfo.setUsername(username);
                        userInfo.setPassword(password);
                        userInfo.setSex(sex);
                        userInfo.setAge(Integer.parseInt(age));
                        userInfo.setPhone(phone);
                        userInfo.setMail(mail);
                        userInfo.setAddress(address);
                        userInfo.setPhoto(photo);
                        temp.add(userInfo);
                    } else {
                        if (temp.size() > 0) {
                            temp.clear();
                        }
                        break;
                    }
                }
            }
        }
        inputStream.close();
        wb.close();
        return temp;
    }
}